package ar.games.ftl;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;



public class jackpot extends Activity implements Runnable {
    protected ImageView img;
    protected ImageView img2;
    protected ImageView img3;
    protected AnimationDrawable frameAnimation;
    protected AnimationDrawable frameAnimation2;
    protected AnimationDrawable frameAnimation3;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        img = (ImageView)findViewById(R.id.spinning_wheel_image); 
        img.setBackgroundResource(R.drawable.spin_animation);
        //a
        img2 = (ImageView)findViewById(R.id.spinning_wheel_image2); 
        img2.setBackgroundResource(R.drawable.spin_animation);
        
        img3 = (ImageView)findViewById(R.id.spinning_wheel_image3); 
        img3.setBackgroundResource(R.drawable.spin_animation);
        
        Button botonInicial = (Button) findViewById(R.id.boton1);
        
        //Load the ImageView that will host the animation and 
        // set its background to our AnimationDrawable XML resource. 
        // Get the background, which has been compiled to an AnimationDrawable object. 

        frameAnimation = (AnimationDrawable) img.getBackground(); 
        frameAnimation2 = (AnimationDrawable) img2.getBackground();
        frameAnimation3 = (AnimationDrawable) img3.getBackground();
        
        botonInicial.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Start the animation (looped playback by default). 
                 
                Spin();
                
            }
        });
    }
    public void run(){
        
        Message myMessage=new Message(); 
        
        if(!frameAnimation.isRunning())
            frameAnimation.start();
        if(!frameAnimation2.isRunning())
            frameAnimation2.start();
        if(!frameAnimation3.isRunning())
            frameAnimation3.start();
        try {
            
            Thread.sleep(900);
            frameAnimation.stop();
            Thread.sleep(250);
            frameAnimation2.stop();
            Thread.sleep(450);
            frameAnimation3.stop();
            
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        myMessage.what = 1;
        handler.sendMessage(myMessage);
        
    }
    private Handler handler = new Handler() {  
        @Override  
        public void handleMessage(Message msg) {  
            // Este codigo refresca la UI dependiendo del resultado del hilo  
           TextView mensaje = (TextView)findViewById(R.id.mensaje); 
           mensaje.setText("Proba de nuevo!");
           frameAnimation = null;
           frameAnimation2 = null;
           frameAnimation3 = null;
           
        }  
     }; 
    private void Spin(){
        //Creo un hilo que se controla la jugada. Creando el hilo puedoe actualizar la UI mientras se ejecuta
       // valFicha = valorFicha;
        //Thread t = new Thread(this);  
        //t.start();
        frameAnimation.start();  
        frameAnimation2.start();
        frameAnimation3.start();
        
         long totalDuration = 0;  
           
         Timer timer = new Timer();  
     
         TimerTask timerTask = new TimerTask(){  
             @Override  
             public void run() {  
                 frameAnimation.stop();  
             }  
         };  
         TimerTask timerTask2 = new TimerTask(){  
             @Override  
             public void run() {  
                 if(frameAnimation.isRunning())
                     frameAnimation.stop();
                 
                 frameAnimation2.stop();  
             }  
         };
         TimerTask timerTask3 = new TimerTask(){  
             @Override  
             public void run() {  
                                
                 if(frameAnimation.isRunning())
                     frameAnimation.stop();
                 if(frameAnimation2.isRunning())
                     frameAnimation2.stop();
                 
                 frameAnimation3.stop();
             }  
         };
         
         
         timer.schedule(timerTask,  450);
         timer.schedule(timerTask2, 900);
         timer.schedule(timerTask3, 1250);
         
    }
}